#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //user
    system_init();
}

MainWindow::~MainWindow()
{
    delete ui;
}


/*---------------------------------------
 *           funtions
 * --------------------------------------*/
void MainWindow::system_init()
{
    //port config
    global_port.setParity(QSerialPort::NoParity);   //固定奇偶校验
    global_port.setDataBits(QSerialPort::Data8);       //固定数据位
    global_port.setStopBits(QSerialPort::OneStop);        //固定停止位

    //connect（）函数 有四个参数使用 1.指向地址2.触发事件（敲击）3.所属类函数4.触发函数
    connect(ui->btn_open,&QPushButton::clicked,this,&MainWindow::btn_open_port);  //打开信号槽
    connect(ui->btn_close,&QPushButton::clicked,this,&MainWindow::btn_close_port); //关闭信号槽
    connect(ui->btn_send,&QPushButton::clicked,this,&MainWindow::btn_send_date);  //数据发送信号槽
    connect(&global_port,&QSerialPort::readyRead,this,&receive_data);             //数据接收信号槽
}
/*---------------------------------------
 *                 slots
 * --------------------------------------*/
void MainWindow::btn_open_port(bool)                //端口号设置
{
    //qDebug()<<ui->cmb_port_name->currentIndex();
    //打开端口号
    switch (ui->cmb_port_name->currentIndex()) {
    case 0:
        global_port.setPortName("COM1");
        break;
    case 1:
        global_port.setPortName("COM2");
        break;
    case 2:
        global_port.setPortName("COM3");
        break;
    case 3:
        global_port.setPortName("COM4");
        break;
    case 4:
        global_port.setPortName("COM5");
        break;
    case 5:
        global_port.setPortName("COM6");
        break;
    case 6:
        global_port.setPortName("COM7");
        break;
    case 7:
        global_port.setPortName("COM8");
        break;
    default:
        break;
    }
    /*-----------------boud rate---------------------*/
    //打开波特率
    switch (ui->cmb_boud_rate->currentIndex()) {
    case 0:
          global_port.setBaudRate(QSerialPort::Baud115200);
        break;
    case 1:
          global_port.setBaudRate(QSerialPort::Baud57600);
        break;
    case 2:
          global_port.setBaudRate(QSerialPort::Baud38400);
        break;
    case 3:
          global_port.setBaudRate(QSerialPort::Baud19200);
        break;
    case 4:
          global_port.setBaudRate(QSerialPort::Baud9600);
        break;
    case 5:
         global_port.setBaudRate(QSerialPort::Baud4800);
        break;
    case 6:
         global_port.setBaudRate(QSerialPort::Baud2400);
        break;
    case 7:
          global_port.setBaudRate(QSerialPort::Baud1200);
        break;
    default:
        break;
    }

    //打开串口
    global_port.open(QIODevice::ReadWrite);
    ui->lab_show->setText("Connect");

    //测试
    //global_port.write("1");



}

/*------------------------------------------------*/
//串口关闭
void MainWindow::btn_close_port(bool)
{
 global_port.close();    //关闭函数
 ui->lab_show->setText("Disconnect");
}



/*-----------------------------------------*/
//串口发送数据
void MainWindow::btn_send_date(bool)
{
      //先拿到数据
   QString DATA =   ui->lin_send_data->text();
     //将数据转换成array格式
   QByteArray array = DATA.toLatin1();
    //发送数据
   global_port.write(array);

}

/*----------------------------------------------*/
//串口接收函数
void MainWindow::receive_data()
{
    QByteArray array = global_port.readAll();   //将接受数据转换

        if(ui->checkBox->checkState() == Qt::Checked)
        {
            ui->Text_receive->insertPlainText(QString(array.toHex(' ').toUpper().append(' ')));
        }
        else
        {
            ui->Text_receive->insertPlainText(QString(array.append(' ')));
        }



}




















